﻿/*
 * Copyright 2009-2011 Edward L. Stauff.                  Contact: <EdStauff@gmail.com>.
 * 
 * This file, "DateTimeParser.cs", is part of the "Stauffware.Common.Time" class library.
 * "Stauffware.Common.Time" is free software: you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * "Stauffware.Common.Time" is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Lesser Public License along with 
 * the "Stauffware.Common.Time" sources.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Globalization;
using Stauffware.Common.Core;

namespace Stauffware.Common.Time
{
	/// <summary>
	/// Parsing options for dates and times.
	/// </summary>
	[Flags]
	public enum DateTimeParseOptions
	{
		/// no options
		None = 0,

		#region ParseOptions compatibility

		/// <summary>
		/// Allow leading whitespace.  (Equal to ParseOptions.AllowLeadingWhite.)
		/// </summary>
		AllowLeadingWhite	= ParseOptions.AllowLeadingWhite,

		/// <summary>
		/// Allow trailing whitespace.  (Equal to ParseOptions.AllowTrailingWhite.)
		/// </summary>
		AllowTrailingWhite	= ParseOptions.AllowTrailingWhite,

		/// <summary>
		/// Allow inner whitespace (but never between digits or time separator).
		/// (Equal to ParseOptions.AllowInnerWhite.)
		/// </summary>
		AllowInnerWhite		= ParseOptions.AllowInnerWhite,

		/// <summary>
		/// Equivalent to combining AllowLeadingWhite, AllowInnerWhite, and AllowTrailingWhite.
		/// (Equal to ParseOptions.AllowWhiteSpace.)
		/// </summary>
		AllowWhiteSpace		= ParseOptions.AllowWhiteSpace,

		/// <summary>
		/// Forces parsing of the entire input string.
		/// Without this option, extraneous characters following a valid time will
		/// terminate the parsing, but not cause an error.
		/// (Equal to ParseOptions.ParseEntireInput.)
		/// </summary>
		ParseEntireInput	= ParseOptions.ParseEntireInput,

		#endregion


		//AdjustToUniversal		= 0x0010,
		//AssumeLocal				= 0x0020,
		//AssumeUniversal			= 0x0040,
		//RoundtripKind			= 0x0080,


		/// Interpret ambiguous day and month numbers as day preceding month.
		DayMonthWhenAmbiguous	= 0x0100,
		/// Interpret ambiguous day and month numbers as month preceding day.
		MonthDayWhenAmbiguous	= 0x0200,
		/// Numeric month must follow day number, even if unambiguous; e.g. "4/31" is not allowed.
		DayMonthStrict			= 0x0400,
		/// Numeric month must precede day number, even if unambiguous; e.g. "31/4" is not allowed.
		MonthDayStrict			= 0x0800,

		/// Allow use of keywords "yesterday", "today", "tomorrow".
		AllowToday				= 0x1000,
		/// Allow dates consisting of a day name with keywords "last", "next".
		AllowRelativeDayOfWeek	= 0x2000,
		/// Allow dates consisting of only a day name; defaults to the date nearest to today.
		DefaultDayToNearestDate	= 0x4000,
		/// Allow dates without years; defaults year to whatever date is nearest to today.
		DefaultYearToNearestDate = 0x8000,
	}

	//#################################################################################
	/// <summary>
	/// A parser for dates and times.
	/// </summary>
	/// <remarks>
	/// This implementation is a temporary hack.
	/// </remarks>

	public class DateTimeParser
	{
		/// <summary>
		/// Enum bits reserved for DateParseOptions.  
		/// "Derived" parse option enums must not redefine these.
		/// </summary>
		internal const int Reserved_ParseOptions_Bits = 0xFFFF00;

		/// <summary>
		/// What to use when there isn't a DateParseOptions parameter.
		/// </summary>
		public const DateTimeParseOptions DefaultParseOptions = 
				DateTimeParseOptions.AllowWhiteSpace | DateTimeParseOptions.ParseEntireInput;

		private readonly DateTimeParseOptions Options;

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Constructs a parser that uses DefaultParseOptions.
		/// </summary>
		public DateTimeParser ( ) : this(DefaultParseOptions, null) { }

		/// <summary>
		/// Constructs a parser.
		/// </summary>
		/// <param name="options">parsing options</param>
		public DateTimeParser (DateTimeParseOptions options) : this(options, null) { }

		/// <summary>
		/// Constructs a parser that uses DefaultParseOptions.
		/// </summary>
		/// <param name="ifp">formatting info, or null</param>
		public DateTimeParser (IFormatProvider ifp) : this(DefaultParseOptions, ifp) { }

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Constructs a parser.
		/// </summary>
		/// <param name="options">parsing options</param>
		/// <param name="ifp">format provider</param>

		public DateTimeParser (DateTimeParseOptions options, IFormatProvider ifp)
		{
			this.Options = options;

			Require.Condition(options.HasFlag(DateTimeParseOptions.ParseEntireInput),
					"sorry, current temporary implementation can't do partial strings");
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Parses a date and time.  
		/// Should never throw an exception except for failed assertions (programming errors).
		/// </summary>
		/// <param name="input">the string to be parsed</param>
		/// <returns>
		///		The result of the parse attempt, whether successful or not; will never return null.
		/// </returns>

		public DateTimeParse Parse (string input)
		{
			// CRAPPY TEMPORARY IMPLEMENTATION

			DateTimeParse parse = new DateTimeParse();

			parse._parsedText = input;

			if (this.Options.HasFlag(DateTimeParseOptions.AllowLeadingWhite))
				input = input.TrimStart();
			if (this.Options.HasFlag(DateTimeParseOptions.AllowTrailingWhite))
				input = input.TrimEnd();

			try
			{
				parse._value = DateTime.Parse(input);
			}
			catch (FormatException e)
			{
				parse._errorMessage = e.Message;
			}

			return parse;
		}

		//-----------------------------------------------------------------------------
	}

	//#################################################################################
	/// <summary>
	/// Encapsulates the result of trying to parse a date ands time from a string into 
	/// a DateTime, whether successful or unsuccessful. 
	/// </summary>

	public class DateTimeParse : ValueParse<DateTime>
	{
	}

	//#################################################################################
}
